package com.zdsoft.wms.interceptor;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class RestTemplateTokenRelay implements ClientHttpRequestInterceptor {
    @Override
    public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
        RequestAttributes requestAttributes= RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes attributes= (ServletRequestAttributes) requestAttributes;
        HttpServletRequest httpServletRequest=attributes.getRequest();
        String token=httpServletRequest.getHeader("X-Token");

        HttpHeaders headers=httpRequest.getHeaders();
        headers.add("X-Token",token);
        return clientHttpRequestExecution.execute(httpRequest,bytes);
    }
}
